﻿using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using ST.Torch.WebApi.Core.Entities.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;


namespace ST.Torch.WebApi.Application.System.Services
{
	public class SYSMenuService : ISYSMenuService, ITransient
	{
		private readonly IRepository<SYSMenu> _SYSMenuRepository;
		public SYSMenuService(IRepository<SYSMenu> SYSMenuRepository)
		{
			_SYSMenuRepository = SYSMenuRepository;
		}

		#region curd
		public  void Insert(SYSMenu entity)
		{
			 _SYSMenuRepository.Insert(entity);
		}
		public async Task InsertAsync(SYSMenu entity, string[] Includes=null)
		{
				await _SYSMenuRepository.InsertAsync(entity);
			
		}
		public void Update(SYSMenu entity, string[] Includes = null)
		{
			if (Includes == null)
				 _SYSMenuRepository.Update(entity);
			else
				 _SYSMenuRepository.UpdateInclude(entity, Includes);
		
		}
		public async Task UpdateAsync(SYSMenu entity, string[] Includes = null)
		{ 
			if (Includes == null)
				await _SYSMenuRepository.UpdateAsync(entity);
			else
				await _SYSMenuRepository.UpdateIncludeAsync(entity, Includes);
		}
		public void Delete(SYSMenu entity, bool IsSoft=false)
		{
			if (IsSoft)
			{
				var fields= entity.Delete();
				Update(entity, fields);
			}
			else
			{
				_SYSMenuRepository.Delete(entity);
			}
				
		}
		public async Task DeleteAsync(SYSMenu entity, bool IsSoft = false)
		{
			if (IsSoft)
			{
				var fields = entity.Delete();
				await UpdateAsync(entity, fields);
			}
			else
			{
				await _SYSMenuRepository.DeleteAsync(entity);
			}

		}
        public bool Any(Expression<Func<SYSMenu, bool>> predicate)
        {
            return _SYSMenuRepository.Any(predicate);
        }
        public async Task<bool> AnyAsync(Expression<Func<SYSMenu, bool>> predicate)
        {
            return await _SYSMenuRepository.AnyAsync(predicate);
        }
        public SYSMenu Single(Expression<Func<SYSMenu, bool>> predicate, bool tracking = false)
		{
			return _SYSMenuRepository.SingleOrDefault(predicate, tracking);

		}
		public async Task<SYSMenu> SingleAsync(Expression<Func<SYSMenu, bool>> predicate, bool tracking = false)
		{
			return await _SYSMenuRepository.SingleOrDefaultAsync(predicate, tracking);

		}
		public List<SYSMenu> Query(Expression<Func<SYSMenu, bool>> predicate, bool tracking = false)
		{
            return _SYSMenuRepository.Where(predicate, tracking).OrderByDescending(u => u.CreateTime).ToList(); 
        }
		public async Task<List<SYSMenu>> QueryAsync(Expression<Func<SYSMenu, bool>> predicate, bool tracking = false)
		{
            return await _SYSMenuRepository.Where(predicate, tracking).OrderByDescending(u => u.CreateTime).ToListAsync(); 
		}

		public PagedList<SYSMenu> QueryPage(Expression<Func<SYSMenu, bool>> predicate, PageReq page, bool tracking = false)
		{
			return _SYSMenuRepository.Where(predicate, tracking).OrderByDescending(u => u.CreateTime).ToPagedList(page.page, page.limit);
		}
		public  async Task<PagedList<SYSMenu>> QueryPageAsync(Expression<Func<SYSMenu, bool>> predicate, PageReq page, bool tracking = false)
		{
			return await _SYSMenuRepository.Where(predicate, tracking).OrderByDescending(u => u.CreateTime).ToPagedListAsync(page.page, page.limit);
		}




		#endregion

	}
}
